package com.tanhua.server.service;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.tanhua.commons.Constants;
import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.MovementApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MqMessageService mqMessageService;


    /**
     *      获取评论列表
     * @param result
     * @param movementId
     * @return
     */
    public PageResult commentsList(PageResult result, ObjectId movementId) {
        PageResult<Comment> pageResult = commentApi.getComment(result,movementId);
        List<Comment> items = pageResult.getItems();
        List<CommentVo> voList = new ArrayList<>();
        for (Comment comment : items) {
            UserInfo userInfo = userInfoApi.getuserinfo(comment.getUserId(), null);
            String key = Constants.MOVEMENTS_INTERACT_KEY + comment.getId();
            String hashkey = Constants.COMMENT_LIKE_HASHKEY + UserHolder.getUserId();
            String like = (String) stringRedisTemplate.opsForHash().get(key, hashkey);
            CommentVo init = CommentVo.init(userInfo, comment);
            if (StringUtils.isNotEmpty(like)) {
                init.setHasLiked(1);
            }
            voList.add(init);
        }
        return new PageResult<>(pageResult.getPage(), pageResult.getPagesize(), pageResult.getCounts(), voList);
    }
    /**
     *      评论动态
     * @param params
     * @return
     */
    public void content(Map<String, String> params) {
        String movementId = params.get("movementId");
        String comment = params.get("comment");
        Movement movement = movementApi.getOneMovement(new ObjectId(movementId));
        UserInfo getuserinfo = userInfoApi.getuserinfo(movement.getUserId(), null);
        Comment comments = new Comment();
        comments.setPublishId(movement.getId());
        comments.setCommentType(CommentType.COMMENT.getType());
        comments.setContent(comment);
        comments.setUserId(UserHolder.getUserId());
        comments.setLikeCount(0);
        comments.setPublishUserId(movement.getUserId());
        comments.setCreated(LocalDateTime.now().toDate().getTime());
        commentApi.putComment(comments);
        mqMessageService.sendLogMsg("0205","comment",movementId);
    }
    /**
     *      点赞评论
     * @return
     */
    public Integer likeComment(ObjectId id) {
        Integer count = commentApi.likeComment(id);
        if (count == null) {
            throw new TanHuaException("服务器响应超时，请重试");
        }
        String key = Constants.MOVEMENTS_INTERACT_KEY + id;
        String hashkey = Constants.COMMENT_LIKE_HASHKEY + UserHolder.getUserId();
        stringRedisTemplate.opsForHash().put(key, hashkey,"1");
        return count;
    }
    /**
     *      取消点赞评论
     * @param id
     * @return
     */
    public Integer dislikeComment(ObjectId id) {
        Integer count = commentApi.dislikeComment(id);
        if (count == null) {
            throw new TanHuaException("服务器响应超时，请重试");
        }
        String key = Constants.MOVEMENTS_INTERACT_KEY + id;
        String hashkey = Constants.COMMENT_LIKE_HASHKEY + UserHolder.getUserId();
        stringRedisTemplate.opsForHash().delete(key, hashkey);
        return count;
    }
}
